<?php
class Dashboard extends Module {
    public function act_show() {
        $url = trim(tos_http_get('url'), '/');
        $params = explode('/', $url);
        
        if ($params && $params[0]) {
            $param1 = $params[0];
            if ($param1[0] == '_') {
                $act = 'act' . $param1;
                unset($params[0]);
            } else {
                $act = 'act_display_node';
            }
        } else {
            $act = 'act_explorer';
            $params = array();
        }
        
        if (method_exists($this, $act)) {
            $this->$act($params);
        } else {
            tos_die('error request');
        }
        /*
        if ($params && $params[0]) {
            $param1 = trim($params[0]);
            if ($param1[0] === '_') {
                $act = 'act' . $param1;
                if (method_exists($this, $act)) {
                    unset($params[0]);
                    $this->$act($params);
                }
            } else {
                if (isset($params[1])) {
                    $parent_url = $params1;
                    $node_url = $params[2];
                } else {
                    $parent_url = '';
                    $node_url = $param1;
                }
                m('node')->act_display(array('node_url' => $node_url, 'parent_url' => $parent_url));
            }
        }
        
        $module_node = m('node');
        $out = out();
        $block_node_search = $module_node->block_search();
        $out->add_block($block_node_search);*/
    }
    
    
    public function act_explorer($params = array()) {
        $out = out();
        $page_index = 1;
        
        /*if (isset($params[1])) {
            $q = $params[1];
        }*/
        if (isset($params[1])) {
            $page_index = (int) $params[1];
        }
        $page_index = ($page_index <= 0) ? 1: $page_index;
        $page_limit = 11;
        
        $module_node = m('node');
        $model_node = $module_node->model('node');
        
        $block_keywords = $module_node->block_keywords('', '');
        $block_nodes_top =$module_node->block_nodes_top();
        $block_nodes_hot = $module_node->block_nodes_hot();
        $block_nodes = $module_node->block_nodes(($page_index - 1) * $page_limit, $page_limit);
        
        
        $block = $this->view('dashboard/dashboard');
        $block->add_block($block_nodes_top, 'left');
        $block->add_block($block_keywords, 'left');
        $block->add_block($block_nodes_hot, 'right');
        $block->add_block($block_nodes, 'nodes');
        $block->add_block(m('html')->block_pagination(array(
            'page_index' => $page_index,
            'page_count' => ceil($model_node->get_count() / $page_limit),
            'base_url'   => '_explorer'
        )), 'pagination');
        out()->add_block($block);
    }
    
    
    public function act_search($params = array()) {
        $out = out();
        $q = '';
        if (isset($params[1])) {
            $q = $params[1];
        }
        $module_node = m('node');
        $block_keywords = $module_node->block_keywords('', $q);
        $block_nodes_top =$module_node->block_nodes_top();
        $block_nodes_hot = $module_node->block_nodes_hot();
        $block_nodes = $module_node->block_nodes_by_search($q);
        
        
        $block = $this->view('dashboard/dashboard');
        $block->add_block($block_nodes_top, 'left');
        $block->add_block($block_keywords, 'left');
        $block->add_block($block_nodes_hot, 'right');
        $block->add_block($block_nodes, 'nodes');
        out()->add_block($block);
    }
    
    public function act_display_node($params = array()) {
        $module_node = m('node');
        /*if (isset($params[1])) {
            $parent_url = $params[0];
            $node_url = $params[1];
        } else {
            $parent_url = '';
            $node_url = $params[0];
        }*/
        $node_url_segment = '';
        if (isset($params[1])) {
            $node_url_segment = $params[1];
        } else {
            $node_url_segment = $params[0];
        }
        $out = out();
        //$block_display_node = m('node')->block_display(array('node_url' => $node_url, 'parent_url' => $parent_url));
        $block_display_node = m('node')->block_display_by_url_segment($node_url_segment);
        
        $out->add_block($block_display_node);
    }
    
    public function act_label($params = array()) {
        $label = '';
        if (isset($params[1])) {
            $label = trim($params[1]);
            $label = str_replace('-', ' ', $label);
            
            $module_node = m('node');
            
            $module_node = m('node');
            $block_keywords = $module_node->block_keywords('', $label);
            $block_nodes_top =$module_node->block_nodes_top();
            $block_nodes_hot = $module_node->block_nodes_hot();
            $block_nodes = $module_node->block_nodes_by_word($label);
            
            
            $block = $this->view('dashboard/dashboard', array('label' => $label));
            $block->add_block($block_nodes_top, 'left');
            $block->add_block($block_keywords, 'left');
            $block->add_block($block_nodes_hot, 'right');
            $block->add_block($block_nodes, 'nodes');
            out()->add_block($block);
            
            
            
            out()->add_block($block);
        }
    }
}
?>